# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 17:36:31 2024

@author: heyuesong
"""


"""
DOM断点

"""

import requests
import pandas as pd
from utils import general

import time
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
import base64

# 假设window.k是一个UTF-8编码的字符串，你需要将其转换为字节串
def sign(t):
    window_k = bytes.fromhex('7764663266662a5447402a284634292a59482967343330485752282a29777365')
    
    # AES加密需要一个固定长度的密钥（对于AES-256，它是32字节）。
    # 如果window.k不是正确的长度，你需要对其进行处理（例如，通过截断或填充）。
    if len(window_k) != 32:
        raise ValueError("Key must be 32 bytes for AES-256")
    
    window_m = window_k  # 在这个例子中，我们假设密钥和窗口m是相同的
    
    def encrypt_word(word):
        # 将输入字符串转换为字节串
        word_bytes = word.encode('utf-8')
        
        # 使用PKCS7填充
        padded_data = pad(word_bytes, AES.block_size)
        
        # 创建一个新的AES cipher对象
        cipher = AES.new(window_m, AES.MODE_ECB)
        
        # 加密数据
        encrypted_data = cipher.encrypt(padded_data)
        
        # 返回base64编码的加密数据
        return base64.b64encode(encrypted_data).decode('utf-8')
    
    # 使用函数
    encrypted_word = encrypt_word(t)
    return encrypted_word


result_list = []
for i in range(1,101):
    print(i)
    
    headers = {
        "authority": "www.python-spider.com",
        "accept": "application/json, text/javascript, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "origin": "https://www.python-spider.com",
        "pragma": "no-cache",
        "referer": "https://www.python-spider.com/challenge/14",
        "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {
        "sessionid": "1uwq6mztqzqy75b9oinixyip9w2u1v4d",
        "Hm_lvt_337e99a01a907a08d00bed4a1a52e35d": "1711520242,1711520884",
        "Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d": "1711593568"
    }
    url = "https://www.python-spider.com/api/challenge14"
    s = str(int(time.time())) + '|' + str(i)
    uc = sign(s)
    data = {
        'page': f'{i}', 
        'uc': uc
     }

    response = requests.post(url, headers=headers, cookies=cookies, data=data,verify=False)
    result = response.json()['data']

    result_list+=result
result_df = pd.DataFrame(result_list)

result_df['value'] = result_df['value'].apply(lambda x:int(x.replace('\r','')))


print(sum(result_df['value']))
